import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { AutomationService } from './automation.service';
import { ViewFilesDto } from './dto/view-files.dto';
import { CreateMatchImportConfigDto } from './dto/create-match-import-config.dto';
import { UpdateMatchImportConfigDto } from './dto/update-match-import-config.dto';
import { ManualImportDto } from './dto/manual-import.dto';
import { QueryImportRecordDto } from './dto/query-import-record.dto';
import { Public } from 'src/common/decorators/public.decorator';

@ApiTags('比赛文件导入管理')
@Controller('automation')
export class AutomationController {
  constructor(private readonly automationService: AutomationService) {}

  // ==================== 导入配置管理 ====================
  
  @ApiOperation({ summary: '创建导入配置' })
  @Public()
  @ApiResponse({ status: 201, description: '创建成功' })
  @Post('configs')
  createConfig(@Body() dto: CreateMatchImportConfigDto) {
    return this.automationService.createConfig(dto);
  }

  @ApiOperation({ summary: '获取所有导入配置' })
  @Public()
  @ApiResponse({ status: 200, description: '获取成功' })
  @Get('configs')
  getAllConfigs() {
    return this.automationService.getAllConfigs();
  }

  @ApiOperation({ summary: '获取单个导入配置' })
  @Public()
  @ApiParam({ name: 'id', description: '配置ID' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Get('configs/:id')
  getConfig(@Param('id') id: string) {
    return this.automationService.getConfig(parseInt(id));
  }

  @ApiOperation({ summary: '更新导入配置' })
  @Public()
  @ApiParam({ name: 'id', description: '配置ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Put('configs/:id')
  updateConfig(@Param('id') id: string, @Body() dto: UpdateMatchImportConfigDto) {
    return this.automationService.updateConfig(parseInt(id), dto);
  }

  @ApiOperation({ summary: '删除导入配置' })
  @Public()
  @ApiParam({ name: 'id', description: '配置ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Delete('configs/:id')
  async deleteConfig(@Param('id') id: string) {
    await this.automationService.deleteConfig(parseInt(id));
    return { message: '删除成功' };
  }

  // ==================== 文件导入操作 ====================

  @ApiOperation({ summary: '手动导入比分文件' })
  @Public()
  @ApiResponse({ status: 200, description: '导入成功' })
  @Post('import/score')
  manualImportScore(@Body() dto: ManualImportDto) {
    return this.automationService.manualImportScore(dto);
  }

  @ApiOperation({ summary: '手动导入首页文件' })
  @Public()
  @ApiResponse({ status: 200, description: '导入成功' })
  @Post('import/homepage')
  manualImportHomepage(@Body() dto: ManualImportDto) {
    return this.automationService.manualImportHomepage(dto);
  }

  @ApiOperation({ summary: '手动触发所有配置的导入任务（首页+比分）' })
  @Public()
  @ApiResponse({ status: 200, description: '触发成功' })
  @Post('import/trigger-all')
  async triggerAllImport() {
    await this.automationService.handleDailyImport();
    return { message: '已触发所有配置的导入任务（首页+比分）' };
  }

  @ApiOperation({ summary: '手动触发所有配置的比分文件导入任务' })
  @Public()
  @ApiResponse({ status: 200, description: '触发成功' })
  @Post('import/trigger-all-score')
  async triggerAllScoreImport() {
    await this.automationService.triggerAllScoreImport();
    return { message: '已触发所有配置的比分文件导入任务' };
  }

  // ==================== 导入记录查询 ====================

  @ApiOperation({ summary: '获取导入记录列表' })
  @Public()
  @ApiResponse({ status: 200, description: '获取成功' })
  @Get('records')
  getImportRecords(@Query() dto: QueryImportRecordDto) {
    return this.automationService.getImportRecords(dto);
  }

  // ==================== 文件管理 ====================

  @ApiOperation({ summary: '查看服务器文件' })
  @Public()
  @ApiResponse({ status: 200, description: '查看成功' })
  @Post('view-files')
  viewFiles(@Body() viewFilesDto: ViewFilesDto) {
    const { filePath, recursive = false, showHidden = false } = viewFilesDto;
    return this.automationService.viewFiles(filePath, recursive, showHidden);
  }
}